#!/usr/bin/perl
use Getopt::Std;
getopts "o:i:e:a:l:s:z:d:";

print "*************\n*$vision*\n*************\n";
if ((!defined $opt_i)|| (!defined $opt_o) ) {
	die "************************************************************************
	Usage:$0 -i filename -o outfile.
	  -h : help and usage.
	  -v : vision.
	  -e : expect value(default 10)
	  -a : identity% (default 0)
	  -l : alignment length (default 0)
	  -s : score (default 0)
	  -z : the block nember of every query	(default 10000)
	  -d : the block nember of every sbjct  (default 10000)
************************************************************************\n";
}
$Expect = (defined $opt_e) ? $opt_e : 10;
$Length = (defined $opt_l) ? $opt_l : 0;
$Identity = (defined $opt_a) ? $opt_a : 0;
$Score = (defined $opt_s) ? $opt_s : 0;
$Query_num = (defined $opt_z) ? $opt_z : 10000;
$Sbjct_num = (defined $opt_d) ? $opt_d : 10000;

open(Ofile,">$opt_o");print "Running.....\n";
open (F,$opt_i) || die"can't open $opt_i:$!\n";
$i=$q=$s=$a=0;
#printf Ofile "%30s %8s %8s %8s %8s %8s %8s %8s %8s %10s %8s %8s\n", "Query-name","Letter","QueryX","QueryY","SbjctX", "SbjctY","Length","Score","E-value","Overlap/total","Identity","Sbject-Name";
printf Ofile "QueryName"."\t"."QueryLength"."\t"."QueryStart"."\t"."QueryEnd"."\t"."SbjctStart"."\t". "SbjctEnd"."\t"."Length"."\t"."Score"."\t"."E-value"."\t"."Overlap/total"."\t"."Identity"."\t"."SbjectName"."\t"."Annotation\n";
while (<F>)
{
	if (/Query=\s+(\S+)/)
	{
		if($i==1)
		{
			#print Ofile "$query^$letter^Query:$qbegin----$qend^Sbject:$sbegin----$send^$name^$annotation^$length^$score^$expect^$identity^$over\n";
			if($score >$Score && $expect<=$Expect && $over>=$Identity && $identity2 >= $Length && $query_num <= $Query_num && $sbjct_num <= $Sbjct_num)
			{
				$ovalap_total = "$identity1/$identity2";
#				printf Ofile "%30s %8s %8s %8s %8s %8s %8s %8s %8s %10s %8s %8s\n",$query,$letter,$qbegin,$qend,$sbegin,$send,$length,$score,$expect,$ovalap_total,$over,$name;
				printf Ofile $query."\t".$letter."\t".$qbegin."\t".$qend."\t".$sbegin."\t".$send."\t".$length."\t".$score."\t".$expect."\t".$ovalap_total."\t".$over."\t".$name."\t".$annotation."\n";
			}
			$i=$q=$s=0;
			$query=$letter=$qbegin=$qend=$sbegin=$send=$name=$annotation=$length=$score=$expect=$identity=$over=0;
		}
		$query=$1;
		$query_num = 0;
	}
	elsif (/\((\S+)\s+letters\)/)
	{
		$letter=$1;$letter=~s/,//;
	}
	elsif (/^>(\S*)\s+(.*)/)
	{
		if($i==1)
		{
			if($score >$Score && $expect<=$Expect && $over>=$Identity && $identity2 >= $Length && $query_num <= $Query_num && $sbjct_num <= $Sbjct_num)
			{
				$ovalap_total = "$identity1/$identity2";
#				printf Ofile "%30s %8s %8s %8s %8s %8s %8s %8s %8s %10s %8s %8s\n",$query,$letter,$qbegin,$qend,$sbegin,$send,$length,$score,$expect,$ovalap_total,$over,$name;
				printf Ofile $query."\t".$letter."\t".$qbegin."\t".$qend."\t".$sbegin."\t".$send."\t".$length."\t".$score."\t".$expect."\t".$ovalap_total."\t".$over."\t".$name."\t".$annotation."\n";

			}
			$i=$q=$s=0;
			$qbegin=$qend=$sbegin=$send=$name=$annotation=$length=$score=$expect=$identity=$over=0;
		}
		$name=$1;
		$annotation=$2;
		$a=1;
		$sbjct_num = 0;
	}
	elsif (/Length = (\d+)/)
	{
		$length=$1;
		$a=0;
	}
	elsif ($a==1)
	{
		chomp;
		$annotation.=$_;
		$annotation=~s/\s+/ /g;
	} #This sentence could get the very long annotation that is longer than one line;
	elsif (/Score = (.+) bits.+Expect\S* =\s+(\S+)\s*/)
	{
		if($i==1)
		{
			if($score >$Score && $expect<=$Expect && $over>=$Identity && $identity2 >= $Length && $query_num <= $Query_num && $sbjct_num <= $Sbjct_num)
			{
				$ovalap_total = "$identity1/$identity2";
#				printf Ofile "%30s %8s %8s %8s %8s %8s %8s %8s %8s %10s %8s %8s\n",$query,$letter,$qbegin,$qend,$sbegin,$send,$length,$score,$expect,$ovalap_total,$over,$name;
				printf Ofile $query."\t".$letter."\t".$qbegin."\t".$qend."\t".$sbegin."\t".$send."\t".$length."\t".$score."\t".$expect."\t".$ovalap_total."\t".$over."\t".$name."\t".$annotation."\n";

			}
			$i=$q=$s=0;
			$qbegin=$qend=$sbegin=$send=$score=$expect=$identity=$over=0;
		}
		$query_num++;
		$sbjct_num++;
		$score=$1;$expect=$2;$expect=~s/^e/1e/;$expect=~s/,//;
	}
	elsif (/Identities = (\d+)\/(\d+)\s+\((.{0,4})%\)/)
	{
		$identity1=$1;
		$identity2=$2;
		$over=$3;
	}
	elsif (/Query\:\s+(\d+)\s+\S+\s+(\d+)/)
	{
		if($q==0)
		{
			$qbegin=$1;
		}
		$qend=$2;
		$q=1;
	}
	elsif (/Sbjct\:\s+(\d+)\s+\S+\s+(\d+)/)
	{
		if($s==0)
		{
			$sbegin=$1;
		}
		$send=$2;
		$s=$i=1;
	}
}
if(($score >$Score && $expect<=$Expect && $over>=$Identity && $identity2 >= $Length && $query_num <= $Query_num && $sbjct_num <= $Sbjct_num)&&($i==1))
{
	$ovalap_total = "$identity1/$identity2";
#	printf Ofile "%30s %8s %8s %8s %8s %8s %8s %8s %8s %10s %8s %8s\n",$query,$letter,$qbegin,$qend,$sbegin,$send,$length,$score,$expect,$ovalap_total,$over,$name;
	printf Ofile $query."\t".$letter."\t".$qbegin."\t".$qend."\t".$sbegin."\t".$send."\t".$length."\t".$score."\t".$expect."\t".$ovalap_total."\t".$over."\t".$name."\t".$annotation."\n";

}
close(F);
close(Ofile);
